<?php

/*
*	$version: 2008-1-3 17:05:51 beta 1.0
*	$id: io.php
*	$author: taoFeng
*	$mailto: font99@Gmail.com
*	$license: This is not a freeware
*/

if (!defined('IN_LEKO')) {header("HTTP/1.0 403 Forbidden");exit;}

class Io
{

	/*
	*	boolean _readFile (String $fileName)
	*/
	public static function _readFile($_f){
		if (function_exists('file_get_contents')){
			return @file_get_contents($_f);
		}
		else
		{
			if (!@$fp = fopen($_f, 'rb')) return false;
			flock($fp, LOCK_SH);
			$result = @fread($fp, filesize($_f));
			fclose($fp);
			if (!$result) return false; else return true;
		}
	}

	/*
	*	boolean _writeFile (String $fileName,String $data)
	*/
	public static function _writeFile($_f,$_c){
		if (function_exists('file_put_contents')){
			if (@file_put_contents($_f, $_c, LOCK_EX)) return true; else return false;
		}
		else
		{
			if (!@$fp = fopen($_f, 'wb')) return false;
			flock($fp, LOCK_UN);
			$result = @fwrite($fp, $_c);
			fclose($fp);
			if (!$result) return false; else return true;
		}
	}

	/*
	*	boolean _appendFile (String $fileName,String $data)
	*/
	public static function _appendFile($_f,$_c){
		if (!@$fp = fopen($_f, 'ab')) return false;
		flock($fp, LOCK_UN);
		$result = @fwrite($fp, $_c);
		fclose($fp);
		if (!$result) return false; else return true;
	}
	
	/*
	*	Array _headFile (String $fileName,[Integer $line,String $crlf])
	*	return first n line
	*/
	public static function _headFile($_f, $_l = 10, $_crlf = "\n"){
		$ret = array();
		if ($_l < 1) return $ret;
		if (!@$fp = fopen($_f, 'rb')) return $ret;

		flock($fp, LOCK_SH);
		for ($i = 0; $i < $_l; $i++){
			$ret[] = @fgets($fp);
			if (feof($fp)) break;
		}
		fclose($fp);
		return $ret;
	}

	/*
	*	Array _tailFile (String $fileName,[Integer $line,String $crlf])
	*	return last n line
	*/
	public static function _tailFile($_f, $_l = 10, $_crlf = "\n"){
		$ret = array();
		$ct = 0;
		$rt = 0;
		if ($_l < 1) return $ret;
		$t_size = 4096 * $_l;
		if (!@$fp = fopen($_f, 'rb')) return $ret;

		flock($fp, LOCK_SH);
		
		while(1){
			fseek($fp, 0 - $t_size * ($ct + 1), SEEK_END);
			$content = fread($fp, $t_size);
			$lines = explode($crlf, rtrim($content, "\r\n"));
			$c_start = true;

			while (1){
				if ($ct > 0 && $crlf != substr($content, 0 - sizeof($crlf)) && $c_start){
					$ret[$n - $rt] = array_pop($lines) . $ret[$n - $rt];
				}
				else{
					$ret[$n - $rt - 1] = array_pop($lines);
					$rt ++;
				}
				$c_start = false;
				if (0 == sizeof($lines) || $rt > $n) break;
			}// end while
			if ($rt > $n || strlen($content) < $t_size) break;
			$ct ++;

		}// end while

		while(--$rt >= $n){
			array_pop($ret);
		}

		$ret = array_reverse($ret);
		fclose($fp);
		return $ret;
	}

	/*
	*	Array _trueTypeFile (String $fileName)
	*	return mime type
	*/
	public static function _trueTypeFile($_f)
	{
		if (function_exists('mime_content_type')) return mime_content_type($_f);
		return false;
	}

	/*
	*	stream _downloadFile (String $fileName,String $downloadName,Boolean $multithread)
	*	//Output 32KB chunk each time.
	*/
	public static function _downloadFile($_f, $_d = '', $_m = false)
	{
		if (!$_d) $_d = basename($_f);

		$filesize = filesize($_f);

		header("Content-type: application/octet-stream");
		header("Content-type:unknown/unknown;");
		header("Content-Disposition: attachment; filename=\"{$_d}\"");
		header('Content-transfer-encoding: binary');

		if ($_m && $range = getenv('HTTP_RANGE'))
		{
			$range = explode('=', $range);
			$range = $range[1];

			header("HTTP/1.1 206 Partial Content");
			header("Date: " . gmdate("D, d M Y H:i:s") . " GMT");
			header("Server: Apache/2.2.0 (Linux) PHP/5.2.4");
			header("Last-Modified: " . gmdate("D, d M Y H:i:s", filemtime($_f)) . " GMT");
			header("Accept-Ranges: bytes");
			header("Content-Length:" . ($filesize - $range));
			header("Content-Range: bytes " . $range . ($filesize - 1) . "/" . $filesize);
			header("Connection: close\n\n");
		}
		else
		{
			header("Content-Length:" . $filesize . "\n\n");
			$range = 0;
		}

		if (!@$fp = fopen($_f, 'wb')) echo '';
		fseek($fp, $range);
		while ($buff = fread($fp, 32768)){
			echo $buff;
		}
		fclose($fp);
		exit;
	}
}

?>
